package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.CityAdapter;
import com.jucai.pppeiyouclient.adapter.RewardAdapter;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.BaseApplication;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.base.TokenParam;
import com.jucai.pppeiyouclient.bean.CityBean;
import com.jucai.pppeiyouclient.bean.RewardBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.SharedPreferencesUtil;
import com.jucai.pppeiyouclient.utils.TextCheckUtils;
import com.jucai.pppeiyouclient.view.dialog.MessageShowDialog;
import com.jucai.pppeiyouclient.weidget.SideBar;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class RewardListActivity extends BaseActivity implements PullToRefreshLayout.OnRefreshListener{
    private static final String TAG = "RewardListActivity : ";
    private PullToRefreshForRecyclerView rewardListRv;
    private List<RewardBean> rewardList;
    private RewardAdapter rewardAdapter;
    private PullToRefreshLayout refreshLayout;

    private Handler mHandler = new Handler();

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_reward_list);
        rewardAdapter = new RewardAdapter(RewardListActivity.this, rewardList);
        ((TextView) findViewById(R.id.common_title_text)).setText("奖励");
        findViewById(R.id.common_home_back).setVisibility(View.INVISIBLE);
        refreshLayout = ((PullToRefreshLayout) findViewById(R.id.refresh_view));
        rewardListRv = (PullToRefreshForRecyclerView) findViewById(R.id.content_view);
        rewardListRv.setLayoutManager(new LinearLayoutManager(RewardListActivity.this, LinearLayoutManager.VERTICAL, false));
        rewardListRv.setItemAnimator(new DefaultItemAnimator());
        rewardListRv.setAdapter(rewardAdapter);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void loadNetData() {
        TokenParam tokenParam = new TokenParam(this, "Accompany_Info.getRewardList");
        CommonRequestNetData.getJsonArrayData(this, tokenParam.getParam(), true, new CommonRequestNetData.JsonArrayCallBack() {
            @Override
            public void parseJsonArray(JSONArray jsonArray) {
                rewardList = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<RewardBean>>() {
                }.getType());
                LogUtils.e(TAG+"jsonarray = " + rewardList.toString());
                rewardAdapter.setData(rewardList);
            }
        });
    }


    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;
            case R.id.common_home_back:
                startActivity(new Intent(RewardListActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        },500);
    }
}
